<template>
  <div class="register-container">
    <el-card class="register-card">
      <h2>用户注册</h2>
      
      <el-form 
        :model="form" 
        :rules="rules" 
        ref="formRef" 
        @submit.prevent="handleRegister"
      >
        <el-form-item prop="username" label="学号">
          <el-input
            v-model="form.username"
            placeholder="请输入学号"
            clearable
          />
        </el-form-item>
        
        <el-form-item prop="pwd" label="密码">
          <el-input
            v-model="form.pwd"
            type="password"
            placeholder="请输入密码"
            show-password
          />
        </el-form-item>
        
        <el-form-item prop="password" label="确认密码">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请再次输入密码"
            show-password
          />
        </el-form-item>
        
        <el-form-item prop="realName" label="姓名">
          <el-input
            v-model="form.realName"
            placeholder="请输入真实姓名"
          />
        </el-form-item>
        
        <el-form-item prop="age" label="年龄">
          <el-input
            v-model.number="form.age"
            type="number"
            placeholder="请输入年龄"
          />
        </el-form-item>
        
        <el-form-item prop="stuNum" label="学号">
          <el-input
            v-model="form.stuNum"
            placeholder="请输入学号"
          />
        </el-form-item>
        
        <el-form-item prop="className" label="班级">
          <el-select
            v-model="form.className"
            placeholder="请选择班级"
            style="width: 100%"
          >
            <el-option label="23软件技术1班" value="23软件技术1班" />
            <el-option label="23软件技术2班" value="23软件技术2班" />
          </el-select>
        </el-form-item>
        
        <el-form-item prop="email" label="邮箱">
          <el-input
            v-model="form.email"
            type="email"
            placeholder="请输入邮箱"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            native-type="submit"
            :loading="loading"
            class="register-button"
          >
            注册
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="footer">
        已有账号？<router-link to="/login">立即登录</router-link>
      </div>
    </el-card>
  </div>
</template>

<script>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../../stores/user'
import { ElMessage } from 'element-plus'

export default {
  setup() {
    const router = useRouter()
    const userStore = useUserStore()
    const formRef = ref(null)
    const loading = ref(false)
    
    const form = ref({
      username: '',
      pwd: '',
      password: '',
      realName: '',
      age: '',
      stuNum: '23003012',
      className: '23软件技术1班',
      email: ''
    })
    
    const validatePassword = (rule, value, callback) => {
      if (value !== form.value.pwd) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    
    const rules = {
      username: [
        { required: true, message: '请输入学号', trigger: 'blur' },
        { min: 8, max: 20, message: '长度在 8 到 20 个字符', trigger: 'blur' }
      ],
      pwd: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请再次输入密码', trigger: 'blur' },
        { validator: validatePassword, trigger: 'blur' }
      ],
      realName: [
        { required: true, message: '请输入真实姓名', trigger: 'blur' }
      ],
      age: [
        { required: true, message: '请输入年龄', trigger: 'blur' },
        { type: 'number', min: 10, max: 100, message: '年龄应在 10 到 100 之间', trigger: 'blur' }
      ],
      stuNum: [
        { required: true, message: '请输入学号', trigger: 'blur' }
      ],
      className: [
        { required: true, message: '请选择班级', trigger: 'change' }
      ],
      email: [
        { required: true, message: '请输入邮箱', trigger: 'blur' },
        { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
      ]
    }
    
    const handleRegister = () => {
      formRef.value.validate(async (valid) => {
        if (valid) {
          loading.value = true
          try {
            await userStore.register(form.value)
            ElMessage.success('注册成功')
            router.push('/login')
          } catch (error) {
            ElMessage.error(error.response?.data?.message || '注册失败')
          } finally {
            loading.value = false
          }
        }
      })
    }
    
    return {
      formRef,
      form,
      rules,
      loading,
      handleRegister
    }
  }
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f5f5;
  padding: 20px;
}

.register-card {
  width: 500px;
  padding: 30px;
}

h2 {
  text-align: center;
  margin-bottom: 30px;
  color: var(--dark-bg);
}

.register-button {
  width: 100%;
}

.footer {
  text-align: center;
  margin-top: 20px;
  color: #666;
}

.footer a {
  color: var(--primary-color);
  text-decoration: none;
}

.footer a:hover {
  text-decoration: underline;
}

@media (max-width: 600px) {
  .register-card {
    width: 100%;
    padding: 20px;
  }
}
</style>